import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';

class XHShare {
  //单例
  static final XHShare _singleton = XHShare._internal();
  get share => _singleton;
  factory XHShare() => _singleton;
  XHShare._internal();

  static double statusBarHeight = Get.statusBarHeight / Get.pixelRatio;
  static double naviBarHeight = Get.statusBarHeight / Get.pixelRatio + 56.0;
  static double bottomSafeHeight = Get.bottomBarHeight / Get.pixelRatio;
  static double screenWidth = Get.width;
  static double screenHeight = Get.height;
  static GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

  ///全局锁定屏幕,禁止自动横屏
  static Future<void> lockScreen() async {
    return SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  }

  /// 打开一个或多个盒子
  /// [keys] 盒子名称列表
  /// [adapters] 适配器列表
  /// `adapters` 列表中的适配器必须继承自 [TypeAdapter],传入的是适配器的实例
  static Future<void> hiveBoxesReady<T extends TypeAdapter>(
      {required List<String> keys, required List<T> adapters}) async {
    await Hive.initFlutter();
    for (var adapter in adapters) {
      Hive.registerAdapter(adapter);
    }
    await Future.wait(keys.map((key) => Hive.openBox(key)));
  }
}
